<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        if ($this->is_logged_in()) { 
            $this->load->model(array('student_model', 'encoder_model', 'instructor_model', 'admin_model'));
            $this->load->helper('url');
            
            $data['unapproved_std'] = $this->student_model->getUnapproved();
            $data['active_status_std'] = $this->student_model->getActivatedStatus();
            $data['active_status_ins'] = $this->instructor_model->getActivatedStatus();
            $data['active_status_enc'] = $this->encoder_model->getActivatedStatus();
            $data['std_list'] = $this->student_model->getAllUsernames();
            $data['ins_list'] = $this->instructor_model->getAllUsernames();
            $data['enc_list'] = $this->encoder_model->getAllUsernames();
            $data['log_list'] = $this->admin_model->getLogs();
            $data['inbox'] = $this->admin_model->getMessages();
            $data['title'] = 'Admin';
            $this->load->view('templates/header', $data);
            $this->load->view('pages/admin_homepage', $data);
            $this->load->view('templates/footer');
        } else {
            redirect('login');
        }
    }

    public function add_user()
    {
        if($this->is_logged_in()) {
            $this->load->library('form_validation');
            
            $role = $this->input->post('role');
            $this->form_validation->set_rules('role','role','trim|required|integer');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            
            switch ($role) {
                case 2:
                    $this->form_validation->set_rules('enc_username','username','trim|required|callback_username_not_exists[2]|max_length[20]');
                    $this->form_validation->set_rules('enc_password','password','trim|required|min_length[6]|max_length[20]|matches[enc_conf_password]');
                    $this->form_validation->set_rules('enc_conf_password', 'password confirmation', 'trim|required|min_length[6]|max_length[20]');
                    $this->form_validation->set_rules('enc_name','name','trim|required|callback_alpha_with_space|max_length[40]');
                    $this->form_validation->set_rules('enc_role','encoder role','trim|required');
                    break;
                case 3:
                    $this->form_validation->set_rules('std_username','username','trim|required|callback_username_not_exists[3]|max_length[20]');
                    $this->form_validation->set_rules('std_password','password','trim|required|min_length[6]|max_length[20]|matches[std_conf_password]');
    				$this->form_validation->set_rules('std_conf_password', 'password confirmation', 'trim|required|min_length[6]|max_length[20]');
    				$this->form_validation->set_rules('std_email','email','trim|required|valid_email');
    				$this->form_validation->set_rules('std_lname','last name','trim|required|callback_alpha_with_space');
                    $this->form_validation->set_rules('std_fname','first name','trim|required|callback_alpha_with_space');
                    $this->form_validation->set_rules('std_mname','middle initial','trim|required|callback_alpha_with_space|max_length[3]');
                    $this->form_validation->set_rules('std_stdno','student number','trim|required|min_length[10]|max_length[10]|callback_stdno_check|callback_valid_std_no');
                    break;
                case 4:
                    $this->form_validation->set_rules('ins_emp_id','employee id','trim|required|integer|min_length[10]|max_length[10]');
                    $this->form_validation->set_rules('ins_username','username','trim|required|callback_username_not_exists[4]|max_length[20]');
                    $this->form_validation->set_rules('ins_password','password','trim|required|min_length[6]|max_length[20]|matches[ins_conf_password]');
                    $this->form_validation->set_rules('ins_conf_password', 'password confirmation', 'trim|required|min_length[6]|max_length[20]');
                    $this->form_validation->set_rules('ins_name','name','trim|required|callback_alpha_with_space|max_length[40]');
                    break;
            }

            if ($this->form_validation->run() == FALSE) {
                $this->index();
            } else {
                switch ($role) {
                    case 2:
                        $username = $this->input->post('enc_username');
                        $password = $this->input->post('enc_password');
                        $name = $this->input->post('enc_name');
                        $role = $this->input->post('enc_role');

                        $data = array(
                            'username' => $username,
                            'password' => md5($password),
                            'name' => $name,
                            'role' => $role,
                            'approved' => 1,
                            'activated' => 1,
                        );
                        $this->load->model('encoder_model');
                        $this->encoder_model->add_account($data);
                        break;
                    case 3:
                        $username = $this->input->post('std_username');
                        $password = $this->input->post('std_password');
    					$email = $this->input->post('std_email');
                        $stdno = $this->input->post('std_stdno');
                        $fname = $this->input->post('std_fname');
                        $mname = $this->input->post('std_mname');
                        $lname = $this->input->post('std_lname');
                        
                        $data = array(
                            'stdno' => $stdno,
                            'name' => $fname . ' ' . $mname . ' ' . $lname,
                            'username' => $username,
                            'password' => md5($password),
    						'email' => $email,
                            'approved' => 1,
                            'activated' => 1
                        );

                        $this->load->model('student_model');
                        $this->student_model->add_account($data);
                        break;
                    case 4:
                        $emp_id = $this->input->post('ins_emp_id');
                        $username = $this->input->post('ins_username');
                        $password = $this->input->post('ins_password');
                        $name = $this->input->post('ins_name');

                        $data = array(
                            'emp_id' => $emp_id,
                            'username' => $username,
                            'password' => md5($password),
                            'name' => $name,
                            'approved' => 1,
                            'activated' => 1
                        );

                        $this->load->model('instructor_model');
                        $this->instructor_model->add_account($data);
                        break;
                }
            }
        } else {
            redirect('login');
        }
    }

    public function approve($username, $role) {
        if ($this->is_logged_in()) {
            switch ($role) {
                case 2:
                    $this->load->model('encoder_model');
                    $this->encoder_model->approve($username);
                    break;
                case 3:
                    $this->load->model('student_model');
                    $this->student_model->approve($username);
                    break;
                case 4:
                    $this->load->model('instructor_model');
                    $this->instructor_model->approve($username);
                    break;
            }
            redirect('admin');
        } else {
            redirect('login');
        }
    }

    public function disapprove($username, $role) {
        if ($this->is_logged_in()) {
            switch ($role) {
                case 2:
                    $this->load->model('encoder_model');
                    $this->encoder_model->delete_account($username);
                    break;
                case 3:
                    $this->load->model('student_model');
                    $this->student_model->delete_account($username);
                    break;
                case 4:
                    $this->load->model('instructor_model');
                    $this->instructor_model->delete_account($username);
                    break;
            }
        } else {
            redirect('login');
        }
    }

    public function activate($username, $role) {
        if ($this->is_logged_in()) {
            switch ($role) {
                case 2:
                    $this->load->model('encoder_model');
                    $this->encoder_model->activate($username);
                    break;
                case 3:
                    $this->load->model('student_model');
                    $this->student_model->activate($username);
                    break;
                case 4:
                    $this->load->model('instructor_model');
                    $this->instructor_model->activate($username);
                    break;
            }
        } else {
            redirect('login');
        }
    }

    public function deactivate($username, $role) {
        if ($this->is_logged_in()) {
            switch ($role) {
                case 2:
                    $this->load->model('encoder_model');
                    $this->encoder_model->deactivate($username);
                    break;
                case 3:
                    $this->load->model('student_model');
                    $this->student_model->deactivate($username);
                    break;
                case 4:
                    $this->load->model('instructor_model');
                    $this->instructor_model->deactivate($username);
                    break;
            }
        } else {
            redirect('login');
        }
    }

    public function delete_user($username, $role) {
        if ($this->is_logged_in()) {
            switch ($role) {
                case 2:
                    $this->load->model('encoder_model');
                    $this->encoder_model->delete_account($username);
                    break;
                case 3:
                    $this->load->model('student_model');
                    $this->student_model->delete_account($username);
                    break;
                case 4:
                    $this->load->model('instructor_model');
                    $this->instructor_model->delete_account($username);
                    break;
            }
        }
    }

    public function logout() {
        session_destroy();
        redirect('login');
    }

    public function sendMessage() {
        $to = $this->input->post('to');
        $message = $this->input->post('message');

        $this->load->model('admin_model');
        $this->admin_model->sendMessage($to, $message);
    }
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */